package com.sample.wanandroidx.basic

import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import androidx.databinding.library.baseAdapters.BR

import androidx.lifecycle.ViewModel
import com.sample.wanandroidx.ClassUtil

import com.sample.wanandroidx.ext.FragmentViewBindingInflater

abstract class BaseMvvmFragment<T : ViewDataBinding, VM : ViewModel>(bindingInflater: FragmentViewBindingInflater<T>) :
    BaseFragment<T>(bindingInflater) {

    protected lateinit var viewModel: VM

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

        viewModel = getActivityViewModel(ClassUtil.getViewModel(this))

        binding.setVariable(BR.viewModel, viewModel)
        binding.lifecycleOwner = this
        binding.executePendingBindings()

        super.onViewCreated(view, savedInstanceState)
    }

    open fun <T : ViewModel> getActivityViewModel(modelClass: Class<T>): T {
        return defaultViewModelProviderFactory.create(modelClass)
    }

}
